import Mock from 'mockjs'

// 菜单数据配置
const menuData = [
  {
    id: 1,
    value: 'dashboard',
    title: '仪表盘',
    icon: 'DashboardIcon',
    path: '/dashboard',
    component: 'dashboard/index',
    sort: 1,
    status: 'active',
    children: []
  },
  {
    id: 2,
    value: 'system',
    title: '系统管理',
    icon: 'SettingIcon',
    path: '/system',
    component: null,
    sort: 2,
    status: 'active',
    children: [
      {
        id: 21,
        value: 'user-management',
        title: '用户管理',
        icon: 'UserIcon',
        path: '/system/users',
        component: 'system/users/index',
        sort: 1,
        status: 'active',
        permission: 'system:user:list'
      },
      {
        id: 22,
        value: 'role-management',
        title: '角色管理',
        icon: 'UserGroupIcon',
        path: '/system/roles',
        component: 'system/roles/index',
        sort: 2,
        status: 'active',
        permission: 'system:role:list'
      },
      {
        id: 23,
        value: 'permission-management',
        title: '权限管理',
        icon: 'LockOnIcon',
        path: '/system/permissions',
        component: 'system/permissions/index',
        sort: 3,
        status: 'active',
        permission: 'system:permission:list'
      },
      {
        id: 24,
        value: 'organization',
        title: '组织架构',
        icon: 'BuildingIcon',
        path: '/system/organization',
        component: 'system/organization/index',
        sort: 4,
        status: 'active',
        permission: 'system:org:list'
      }
    ]
  },
  {
    id: 3,
    value: 'operations',
    title: '运营管理',
    icon: 'ShopIcon',
    path: '/operations',
    component: null,
    sort: 3,
    status: 'active',
    children: [
      {
        id: 31,
        value: 'customer-management',
        title: '客户管理',
        icon: 'UserIcon',
        path: '/operations/customers',
        component: 'operations/customers/index',
        sort: 1,
        status: 'active',
        permission: 'operations:customer:list'
      },
      {
        id: 32,
        value: 'supplier-management',
        title: '供应商管理',
        icon: 'TruckIcon',
        path: '/operations/suppliers',
        component: 'operations/suppliers/index',
        sort: 2,
        status: 'active',
        permission: 'operations:supplier:list'
      },
      {
        id: 33,
        value: 'product-management',
        title: '产品管理',
        icon: 'PackageIcon',
        path: '/operations/products',
        component: 'operations/products/index',
        sort: 3,
        status: 'active',
        permission: 'operations:product:list'
      }
    ]
  },
  {
    id: 4,
    value: 'inventory',
    title: '库存管理',
    icon: 'BoxIcon',
    path: '/inventory',
    component: null,
    sort: 4,
    status: 'active',
    children: [
      {
        id: 41,
        value: 'stock-in',
        title: '入库管理',
        icon: 'ArrowDownIcon',
        path: '/inventory/stock-in',
        component: 'inventory/stock-in/index',
        sort: 1,
        status: 'active',
        permission: 'inventory:stockin:list'
      },
      {
        id: 42,
        value: 'stock-out',
        title: '出库管理',
        icon: 'ArrowUpIcon',
        path: '/inventory/stock-out',
        component: 'inventory/stock-out/index',
        sort: 2,
        status: 'active',
        permission: 'inventory:stockout:list'
      },
      {
        id: 43,
        value: 'stock-query',
        title: '库存查询',
        icon: 'SearchIcon',
        path: '/inventory/query',
        component: 'inventory/query/index',
        sort: 3,
        status: 'active',
        permission: 'inventory:query:list'
      }
    ]
  },
  {
    id: 5,
    value: 'purchase',
    title: '采购管理',
    icon: 'ShoppingCartIcon',
    path: '/purchase',
    component: null,
    sort: 5,
    status: 'active',
    children: [
      {
        id: 51,
        value: 'purchase-order',
        title: '采购订单',
        icon: 'FileTextIcon',
        path: '/purchase/orders',
        component: 'purchase/orders/index',
        sort: 1,
        status: 'active',
        permission: 'purchase:order:list'
      },
      {
        id: 52,
        value: 'purchase-plan',
        title: '采购计划',
        icon: 'CalendarIcon',
        path: '/purchase/plans',
        component: 'purchase/plans/index',
        sort: 2,
        status: 'active',
        permission: 'purchase:plan:list'
      }
    ]
  },
  {
    id: 6,
    value: 'sales',
    title: '销售管理',
    icon: 'TrendingUpIcon',
    path: '/sales',
    component: null,
    sort: 6,
    status: 'active',
    children: [
      {
        id: 61,
        value: 'sales-order',
        title: '销售订单',
        icon: 'FileTextIcon',
        path: '/sales/orders',
        component: 'sales/orders/index',
        sort: 1,
        status: 'active',
        permission: 'sales:order:list'
      },
      {
        id: 62,
        value: 'sales-plan',
        title: '销售计划',
        icon: 'TargetIcon',
        path: '/sales/plans',
        component: 'sales/plans/index',
        sort: 2,
        status: 'active',
        permission: 'sales:plan:list'
      }
    ]
  },
  {
    id: 7,
    value: 'finance',
    title: '财务管理',
    icon: 'MoneyIcon',
    path: '/finance',
    component: null,
    sort: 7,
    status: 'active',
    children: [
      {
        id: 71,
        value: 'accounts-receivable',
        title: '应收账款',
        icon: 'CreditCardIcon',
        path: '/finance/receivables',
        component: 'finance/receivables/index',
        sort: 1,
        status: 'active',
        permission: 'finance:receivable:list'
      },
      {
        id: 72,
        value: 'accounts-payable',
        title: '应付账款',
        icon: 'CreditCardIcon',
        path: '/finance/payables',
        component: 'finance/payables/index',
        sort: 2,
        status: 'active',
        permission: 'finance:payable:list'
      }
    ]
  },
  {
    id: 8,
    value: 'logistics',
    title: '物流管理',
    icon: 'TruckIcon',
    path: '/logistics',
    component: null,
    sort: 8,
    status: 'active',
    children: [
      {
        id: 81,
        value: 'transport-order',
        title: '运输订单',
        icon: 'TruckIcon',
        path: '/logistics/transport',
        component: 'logistics/transport/index',
        sort: 1,
        status: 'active',
        permission: 'logistics:transport:list'
      },
      {
        id: 82,
        value: 'route-planning',
        title: '路线规划',
        icon: 'MapIcon',
        path: '/logistics/routes',
        component: 'logistics/routes/index',
        sort: 2,
        status: 'active',
        permission: 'logistics:route:list'
      }
    ]
  },
  {
    id: 9,
    value: 'reports',
    title: '报表分析',
    icon: 'ChartIcon',
    path: '/reports',
    component: null,
    sort: 9,
    status: 'active',
    children: [
      {
        id: 91,
        value: 'business-report',
        title: '业务报表',
        icon: 'FileTextIcon',
        path: '/reports/business',
        component: 'reports/business/index',
        sort: 1,
        status: 'active',
        permission: 'reports:business:list'
      },
      {
        id: 92,
        value: 'financial-report',
        title: '财务报表',
        icon: 'TrendingUpIcon',
        path: '/reports/financial',
        component: 'reports/financial/index',
        sort: 2,
        status: 'active',
        permission: 'reports:financial:list'
      }
    ]
  },
  {
    id: 10,
    value: 'profile',
    title: '个人中心',
    icon: 'UserCircleIcon',
    path: '/profile',
    component: 'profile/index',
    sort: 10,
    status: 'active',
    children: []
  }
]

// 获取菜单列表
Mock.mock('/api/v1/menus', 'get', () => {
  return {
    code: 200,
    message: '获取成功',
    data: menuData
  }
})

// 根据用户角色获取菜单
Mock.mock('/api/v1/menus/user', 'get', (options) => {
  // 从请求头获取用户角色，mock中返回所有菜单
  const userMenus = menuData.filter(menu => menu.status === 'active')
  
  return {
    code: 200,
    message: '获取成功',
    data: userMenus
  }
})

// 获取菜单树形结构
Mock.mock('/api/v1/menus/tree', 'get', () => {
  const buildTree = (items, parentId = null) => {
    return items
      .filter(item => {
        if (parentId === null) {
          return !item.parentId
        }
        return item.parentId === parentId
      })
      .map(item => ({
        ...item,
        children: buildTree(items, item.id)
      }))
  }
  
  const treeData = buildTree(menuData)
  
  return {
    code: 200,
    message: '获取成功',
    data: treeData
  }
})

// 创建菜单
Mock.mock('/api/v1/menus', 'post', (options) => {
  const menuData = JSON.parse(options.body)
  
  const newMenu = {
    id: Date.now(),
    ...menuData,
    createTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss'),
    updateTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
  }
  
  // 添加到菜单列表
  menuData.push(newMenu)
  
  return {
    code: 200,
    message: '创建成功',
    data: newMenu
  }
})

// 更新菜单
Mock.mock(/\/api\/v1\/menus\/\d+$/, 'put', (options) => {
  const url = options.url
  const menuId = parseInt(url.match(/\/menus\/(\d+)$/)[1])
  const updateData = JSON.parse(options.body)
  
  const menuIndex = menuData.findIndex(m => m.id === menuId)
  
  if (menuIndex === -1) {
    return {
      code: 404,
      message: '菜单不存在',
      data: null
    }
  }
  
  // 更新菜单信息
  menuData[menuIndex] = {
    ...menuData[menuIndex],
    ...updateData,
    updateTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
  }
  
  return {
    code: 200,
    message: '更新成功',
    data: menuData[menuIndex]
  }
})

// 删除菜单
Mock.mock(/\/api\/v1\/menus\/\d+$/, 'delete', (options) => {
  const url = options.url
  const menuId = parseInt(url.match(/\/menus\/(\d+)$/)[1])
  
  const menuIndex = menuData.findIndex(m => m.id === menuId)
  
  if (menuIndex === -1) {
    return {
      code: 404,
      message: '菜单不存在',
      data: null
    }
  }
  
  // 检查是否有子菜单
  const hasChildren = menuData.some(m => m.parentId === menuId)
  if (hasChildren) {
    return {
      code: 400,
      message: '请先删除子菜单',
      data: null
    }
  }
  
  // 删除菜单
  menuData.splice(menuIndex, 1)
  
  return {
    code: 200,
    message: '删除成功',
    data: null
  }
})

// 获取用户权限
Mock.mock('/api/v1/auth/permissions', 'get', () => {
  // 根据用户角色返回权限列表
  const permissions = [
    'dashboard:view',
    'system:user:list',
    'system:user:create',
    'system:user:update',
    'system:user:delete',
    'system:role:list',
    'system:role:create',
    'system:role:update',
    'system:role:delete',
    'system:permission:list',
    'system:org:list',
    'operations:customer:list',
    'operations:supplier:list',
    'operations:product:list',
    'inventory:stockin:list',
    'inventory:stockout:list',
    'inventory:query:list',
    'purchase:order:list',
    'purchase:plan:list',
    'sales:order:list',
    'sales:plan:list',
    'finance:receivable:list',
    'finance:payable:list',
    'logistics:transport:list',
    'logistics:route:list',
    'reports:business:list',
    'reports:financial:list'
  ]
  
  return {
    code: 200,
    message: '获取成功',
    data: permissions
  }
})

// 检查用户是否有某个权限
Mock.mock('/api/v1/auth/check-permission', 'post', (options) => {
  const { permission } = JSON.parse(options.body)
  
  const permissions = [
    'dashboard:view',
    'system:user:list',
    'system:user:create',
    'system:user:update',
    'system:user:delete',
    'system:role:list',
    'system:role:create',
    'system:role:update',
    'system:role:delete',
    'system:permission:list',
    'system:org:list',
    'operations:customer:list',
    'operations:supplier:list',
    'operations:product:list',
    'inventory:stockin:list',
    'inventory:stockout:list',
    'inventory:query:list',
    'purchase:order:list',
    'purchase:plan:list',
    'sales:order:list',
    'sales:plan:list',
    'finance:receivable:list',
    'finance:payable:list',
    'logistics:transport:list',
    'logistics:route:list',
    'reports:business:list',
    'reports:financial:list'
  ]
  
  const hasPermission = permissions.includes(permission)
  
  return {
    code: 200,
    message: hasPermission ? '权限验证通过' : '权限不足',
    data: {
      hasPermission,
      permission
    }
  }
}) 